<?php
/**
 * Created by PhpStorm.
 * User: xiangfeng
 * Date: 2022/4/9
 * Time: 3:48 PM
 */

namespace app\api\controller;


use app\api\http\exceptions\ApiErr;
use app\api\http\exceptions\ApiExceptions;
use app\api\service\ArticleService;


/**
 * 校友活动
 * Class Article
 * @package app\api\controller
 */
class Article extends BaseApi
{
    /**
     *  1、文章列表
     * @param int $type_id
     * @return \think\response\Json
     * @throws ApiExceptions
     * @throws \think\db\exception\DbException
     */
    public function getList(int $type_id = 0)
    {
        $param = $this->app->request->param();
        if ($type_id <= 0) {
            throw new ApiExceptions(ApiErr::ERROR_PARAMS);
        }
        $page = isset($param['cur_page']) ? (int)$param['cur_page'] : 1;
        $limit = isset($param['cur_size']) ? (int)$param['cur_size'] : 20;
        $page_config = [
            "list_rows" => $limit,
            "page" => $page
        ];
        $data = ArticleService::getArticleList($page_config, ['article_type_id' => $type_id]);
        return $this->jsonSuccessResponse($data);
    }

    /**
     *  2、文章详情
     * @param int $id
     * @return \think\response\Json
     * @throws ApiExceptions
     */
    public function getInfo(int $id = 0)
    {
        if ($id <= 0) {
            throw new ApiExceptions(ApiErr::ERROR_PARAMS);
        }
        $data = ArticleService::getArticleInfo($id);
        return $this->jsonSuccessResponse($data);
    }

    /**
     *  3、资讯列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getNewsList()
    {
        $param = $this->app->request->param();
        $page = isset($param['cur_page']) ? (int)$param['cur_page'] : 1;
        $limit = isset($param['cur_size']) ? (int)$param['cur_size'] : 20;
        $page_config = [
            "list_rows" => $limit,
            "page" => $page
        ];
        $data = ArticleService::getNewsList($page_config);
        return $this->jsonSuccessResponse($data);
    }
}